package swing.dialog;

import api.UserApi;
import beans.Word;
import client.UserSession;
import http.Response;
import model.ApiResultModel;
import myException.ServerConnectException;

import javax.swing.*;
import java.awt.*;

public class WordDetailDialog extends JDialog {

    public JPanel wordDetailPanel;

    public JLabel wordStaticText;
    public JLabel wordText;
    public JLabel userStaticText;
    public JLabel createUser;
    public JLabel timeStaticText;
    public JLabel createTime;

    public Word word;

    public WordDetailDialog(JFrame parentFrame, Word word) {
        super(parentFrame, false);
        this.word = word;
        this.setTitle("单词详情");
        this.setSize(350, 400);
        this.setLocationRelativeTo(parentFrame);
        wordDetailPanel = new JPanel();
        wordDetailPanel.setLayout(null);
        initAllComponent();
        addAllComponent();
        this.setContentPane(wordDetailPanel);
    }

    private void initAllComponent() {
        //静态文本
        wordStaticText = new JLabel("单词：");
        Font staticTextFont = new Font("宋体", Font.BOLD,20);
        wordStaticText.setBounds(15, 15, 200, 20);
        wordStaticText.setFont(staticTextFont);
        //单词
        wordText = new JLabel(word.getWord_string());
        wordText.setBounds(15, 45, 300, 20);
        Font font = new Font("宋体", Font.PLAIN,17);
        wordText.setFont(font);
        //静态文本
        userStaticText = new JLabel("创建者：");
        userStaticText.setBounds(15, 90, 200, 20);
        userStaticText.setFont(staticTextFont);
        //单词创建者
        createUser = new JLabel();
        String userName = queryUserName(word.getCreate_user());
        createUser.setText(userName);
        createUser.setBounds(15, 130, 200, 20);
        createUser.setFont(font);
        //静态文本
        timeStaticText = new JLabel("创建时间：");
        timeStaticText.setBounds(15, 170, 200, 20);
        timeStaticText.setFont(staticTextFont);
        //单词创建时间
        createTime = new JLabel(word.getCreate_time().toString());
        createTime.setBounds(15, 200, 400, 20);
        createTime.setFont(font);
    }

    private void addAllComponent() {
        wordDetailPanel.add(wordStaticText);
        wordDetailPanel.add(wordText);
        wordDetailPanel.add(userStaticText);
        wordDetailPanel.add(createUser);
        wordDetailPanel.add(timeStaticText);
        wordDetailPanel.add(createTime);
    }

    public String queryUserName(long query_user) {
        try {
            Response response = UserApi.query_user_name(UserSession.LoginUser(), query_user);
            ApiResultModel apiResultModel = ApiResultModel.fromJson(response.getBody());
            if (apiResultModel.getCode() == ApiResultModel.SUCCESS_CODE) {
                return  (String)apiResultModel.getResult();
            }
        } catch (ServerConnectException e) {
            showMessageDialog("服务器连接失败");
            this.dispose();
            e.printStackTrace();
        }
        return null;
    }



    private void showMessageDialog(String message) {
        JOptionPane.showMessageDialog(wordDetailPanel, message);
    }
}
